// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html

var OnTapEvent = require('OnTapEvent');
var OnGridLoaded = require('OnGridLoaded');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        line: {
            type: cc.Prefab,
            default: null
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start () {
        var w = this.node.width;
        var h = this.node.height;

        var lineH = 2;
        this.lineNum = 9;
        this.dist = (w - lineH * this.lineNum) / (this.lineNum - 1);
        var xx = -w * 0.5 + lineH * 0.5;
        var yy = xx;

        this.dd = w / (this.lineNum - 1);

        for (i = 0; i < this.lineNum; i++){
            var line = cc.instantiate(this.line);
            line.width = w;
            line.height = lineH;
            line.x = 0;
            line.y = yy;
            this.node.addChild(line);
            yy += this.dist + lineH;

            line = cc.instantiate(this.line);
            line.width = lineH;
            line.height = h;
            line.x = xx;
            line.y = 0;
            this.node.addChild(line);
            xx += this.dist + lineH;
        }

        this.node.on(cc.Node.EventType.TOUCH_END, function(e){
            this.onTouch(e);
        }.bind(this), this);

        var onGridLoad = new OnGridLoaded();
        onGridLoad.init(this.dd, this.node.width);
        this.node.dispatchEvent(onGridLoad);
    },
    onTouch(event){
        var pos = this.node.convertToNodeSpaceAR(event.getLocation());
        var halfW = this.node.width * 0.5;
        pos.x += halfW;
        pos.y += halfW;
        
        var h = Math.floor(pos.x / this.dd + 0.5);
        var v = Math.floor(pos.y / this.dd + 0.5);

        var tapEvent = new OnTapEvent();
        tapEvent.init(h, v);
        this.node.dispatchEvent(tapEvent);
    }

    // update (dt) {},
});
